<?php

/**
 * Smer form.
 *
 * @package    dnevnik
 * @subpackage form
 * @author     Your name here
 * @version    SVN: $Id: sfPropelFormTemplate.php 10377 2008-07-21 07:10:32Z dwhittle $
 */
class SmerForm extends BaseSmerForm
{
  public function configure()
  {
      $this->widgetSchema['tip'] = new sfWidgetFormChoice(array('choices' => array(SmerPeer::getTips())));
      $this->widgetSchema['predmet_smer'] = new sfWidgetFormPropelChoice(array(
      'choices' => PredmetPeer::doSelect(new Criteria()),
      'multiple' => true,
      'model' => 'Predmet',
    ));

    $this->widgetSchema['predmet_smer']->setOption('renderer_class', 'sfWidgetFormSelectDoubleList');

    $this->validatorSchema['predmet_smer'] = new sfValidatorPropelChoice(array(
      'multiple' => true,
      'model' => 'Predmet',
    ));
  }

  public function updateDefaultsFromObject()
  {
    parent::updateDefaultsFromObject();

    if (isset($this->widgetSchema['predmet_smer']) && !$this->isNew())
    {
      foreach ($this->object->getPredmetSmers() as $obj)
      {
        $values[] = $obj->getPredmetId();
      }
      $this->setDefault('predmet_smer', $values);
    }

  }

  protected function doSave($con = null)
  {
    parent::doSave($con);
    $this->savePrecelProjectCategoryMatchList($con);
  }

  public function savePrecelProjectCategoryMatchList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['predmet_smer']))
    {
      // somebody has unset this widget
      return;
    }

    if (is_null($con))
    {
      $con = $this->getConnection();
    }

    $predmeti = array();
    $tst_razred_smera = DbFinder::from('Razred')->where('SmerId', $this->object->getPrimaryKey())->findOne();
    $predmet_smers = DbFinder::from('PredmetSmer')->findBy('SmerId', $this->object->getId());
    $razred_smers = DbFinder::from('Razred')->where('SmerId', $this->object->getPrimaryKey())->find();
    foreach($predmet_smers as $predmet_smer)
      $predmeti[] = $predmet_smer->getPredmetId();

    $values = $this->getValue('predmet_smer');
    $dodati = array_diff($values, $predmeti);
    $brisati = array_diff($predmeti, $values);

    if (is_array($dodati))
    {
      foreach ($dodati as $value)
      {
        $obj = new PredmetSmer();
        $obj->setSmerId($this->object->getPrimaryKey());
        $obj->setPredmetId($value);
        $obj->save();
        if($tst_razred_smera)
        if(!DbFinder::from('PredmetRazred')->where('PredmetId', $value)->where('RazredId', $tst_razred_smera->getId())->count())
        {
            foreach($razred_smers as $razred_smer)
            {
                $predmet_razred = new PredmetRazred();
                $predmet_razred->setPredmetId($value);
                $predmet_razred->setRazredId($razred_smer->getId());
                $predmet_razred->save();
            }
        }
      }
    }

    if(is_array($brisati))
    {
        foreach($brisati as $value)
        {
            $predmet_smers = DbFinder::from('PredmetSmer')->where('SmerId', $this->object->getId())->where('PredmetId', $value)->delete();
            foreach($razred_smers as $razred_smer)
            {
                $predmet_razreds = DbFinder::from('PredmetRazred')->where('RazredId', $razred_smer->getId())->where('PredmetId', $value)->delete();
            }
        }
    }

    $presek = array_intersect($predmeti, $values);
  }
}
